//
//  MTCCreateProjectViewController.m
//  ERP
//
//  Created by QA on 15/9/22.
//  Copyright (c) 2015年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "MTCCreateProjectViewController.h"
#import "GEInputTableViewCell.h"
#import "NSDictionary+DataFormat.h"

@interface MTCCreateProjectViewController ()

@property (nonatomic, strong) NSMutableDictionary *projectInfo;
@property (nonatomic, strong) NSDictionary *contractInfo;

@end

@implementation MTCCreateProjectViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(sendButtonEvent:)];
        self.navigationItem.rightBarButtonItem = leftButton;
    }
    return self;
}

- (void)getPassingParameter:(id)object
{
    if (!object) return;
    if ([object isKindOfClass:[NSArray class]]) {
        
        NSArray *array = object;
        self.contractInfo = [array firstObject];
        self.projectInfo = array.count >= 2 ? [array lastObject] : nil;
    }
    
    if (!self.projectInfo) {
        [self setTitle: @"新增合同工程"];
    } else {
        [self setTitle: @"修改合同工程"];
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 10)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 10)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)sendButtonEvent:(UIButton *)button
{
    UITextField *textField00 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:0];
    if (textField00.text.length <= 0) {
        [self showAlertView:@"请输入工程名称" delegate:nil];
    } else {
        [self submitNewProject];
    }
}

#pragma mark 网络提交数据

- (void)submitNewProject
{
    UITextField *textField00 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:0];
    UITextField *textField01 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:0];
    UITextField *textField02 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:0];
    
    UITextField *textField03 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:1];
    UITextField *textField04 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:1];
    UITextField *textField05 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:1];
    UITextField *textField06 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:3 inSection:1];
    UITextField *textField07 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:4 inSection:1];
    
    //客户编号处理
    NSString *projectNumber = @"0";
    if (self.projectInfo) {
        projectNumber = [self.projectInfo objectFormatStringForKey:@"ID"];
    }
    
    //合同ID
    NSString *concreteID = [self.contractInfo objectFormatStringForKey:@"合同ID"];
    
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setShowLoadingPrompt:self.navigationController labelText:@"提交工程信息中"];
    [content setInterfaceType:InterfaceTypeGroup];
    [content setModuleType:@"HTGL_GCMC_AU"];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    
    [content addParameterKey:@"ID" value:projectNumber];
    [content addParameterKey:@"合同ID" value:concreteID];
    [content addParameterKey:@"工程名称" value:textField00.text];
    [content addParameterKey:@"建设单位" value:textField01.text];
    [content addParameterKey:@"施工单位" value:textField02.text];
    [content addParameterKey:@"运输距离" value:textField03.text defaults:@"0"];
    [content addParameterKey:@"工地地址" value:textField04.text];
    [content addParameterKey:@"工地联系人" value:textField05.text];
    [content addParameterKey:@"工地联系电话" value:textField06.text];
    [content addParameterKey:@"备注" value:textField07.text];
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        NSArray *firstArray = nil;
        if(result.resultArray && result.resultArray.count>0)
            firstArray = [result.resultArray firstObject];
        
        if(firstArray && firstArray.count>0){
            [self showAlertView:@"提交成功" delegate:self];
            NSDictionary *valueDic = [firstArray firstObject];
            if(self.backResultDelagate) [self.backResultDelagate backResultDataKey:self.key dataValue:valueDic];
        } else {
            [self showAlertView:@"提交工程信息失败，请检查填写内容是否正确" delegate:nil];
        }
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self showAlertView:@"系统繁忙，请稍后再试" delegate:nil];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self showAlertView:@"网络连接失败，请检查网络是否正常" delegate:nil];
    }];
    
}

#pragma mark - UIAlertView

- (void)showAlertView:(NSString*)message delegate:(id)delegate
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:message delegate:delegate cancelButtonTitle:@"确定" otherButtonTitles:nil];
    alert.alertViewStyle=UIAlertViewStyleDefault;
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0) return 3;
    if(section == 1) return 5;
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"CreateContractViewController%ld%ld",indexPath.section,indexPath.row];
    
    //合同基本信息
    if(indexPath.section == 0){
        GEInputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell != nil) return cell;
        
        if(indexPath.row == 0){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"工程名称";
            cell.placeholder = @"请输入工程名称";
            cell.isMark = YES;
            cell.isEdit = YES;
            if (self.projectInfo) cell.inputFiled = [self.projectInfo objectFormatStringForKey:@"工程名称"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 1){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"建设单位";
            cell.placeholder = @"请输入建设单位";
            cell.isMark = NO;
            cell.isEdit = YES;
            if (self.projectInfo) cell.inputFiled = [self.projectInfo objectFormatStringForKey:@"建设单位"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 2){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"施工单位";
            cell.placeholder = @"请输入施工单位";
            cell.isMark = NO;
            cell.isEdit = YES;
            if (self.projectInfo) cell.inputFiled = [self.projectInfo objectFormatStringForKey:@"施工单位"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        }
        return cell;
    }
    
    //其他信息
    else if(indexPath.section == 1){
        GEInputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell != nil) return cell;
        else if(indexPath.row == 0){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"工地距离";
            cell.placeholder = @"请输入工地距离(公里)";
            cell.isMark = NO;
            cell.isEdit = YES;
            [cell setKeyboardType:UIKeyboardTypeDecimalPad filter:@"0123456789."];
            if (self.projectInfo) cell.inputFiled = [self.projectInfo objectFormatStringForKey:@"运输距离"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 1){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"工地地址";
            cell.placeholder = @"请输入工地地址";
            cell.isMark = NO;
            cell.isEdit = YES;
            if (self.projectInfo) cell.inputFiled = [self.projectInfo objectFormatStringForKey:@"工地地址"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 2){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"工地联系人";
            cell.placeholder = @"请输入更多联系人";
            cell.isMark = NO;
            cell.isEdit = YES;
            if (self.projectInfo) cell.inputFiled = [self.projectInfo objectFormatStringForKey:@"工地联系人"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 3){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"工地联系电话";
            cell.placeholder = @"请输入工地联系电话";
            cell.isMark = NO;
            cell.isEdit = YES;
            [cell setKeyboardType:UIKeyboardTypeDecimalPad filter:@"0123456789+"];
            if (self.projectInfo) cell.inputFiled = [self.projectInfo objectFormatStringForKey:@"工地联系电话"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 4){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"备        注";
            cell.placeholder = @"可不填";
            cell.isMark = NO;
            cell.isEdit = YES;
            if (self.projectInfo) cell.inputFiled = [self.projectInfo objectFormatStringForKey:@"备注"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
        }
        
        return cell;
    }
    
    return nil;
}

#pragma mark
#pragma mark - UITableViewDelegate

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //键盘事件处理
    UITextField *textField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:indexPath.row inSection:indexPath.section];
    if (textField) [textField becomeFirstResponder];
    
}

@end
